package com.ttt.adbgui.function;

import com.ttt.adbgui.jcomp.JFrameComponent;
import com.ttt.adbgui.utils.JavaCMD;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;

/*
* 操作文件列表
*/

@Component
@Slf4j
public class DocumentList {

    String homeCmd = "cmd /c adb shell ls /";
    String cmdStart = "cmd /c ";
    String devices = "cmd /c adb devices";

    @Autowired
    private JavaCMD javaCMD;
    @Autowired
    private JFrameComponent jFrameComponent;

    // 获取根目录的文件列表
    public ArrayList getHomeList(){
        String deviceName = jFrameComponent.getDeviceName();
        String cmd = cmdStart + "adb -s " + deviceName + " shell ls /";
        log.info(cmd);
        ArrayList<String> strings = javaCMD.execDefault(cmd);
        return strings;
    }

    // 获取当前已连接设备
    public ArrayList getConnectDevices(){
        ArrayList<String> strings = javaCMD.execDefault(devices);
        ArrayList<String> resultStr = jFrameComponent.getResultStr();
        if (strings.size() > 1){
            for (int i = 1; i < strings.size(); i++) {
                String startIndex = strings.get(i).split("\\t")[0];
                resultStr.add(startIndex);
            }
        }
        return resultStr;
    }

    // 获取所选目录的文件列表
    public ArrayList getSelectedDirectory(){
        ArrayList<String> str = javaCMD.execDefault(homeCmd);
        return str;
    }

}
